package com.aiss.ws.httptools;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 
 * @author 马辰希
 * 
 */
public class GetStringtPostUtil {

	private HttpHandler<String> handler;

	/**
	 * 请求String
	 * 
	 * @param url
	 * @param map
	 */
	public GetStringtPostUtil(String url, HashMap<String, String> map,
							  final GetStringListener listener) {
		super();
		RequestParams params = new RequestParams();
		Set<Entry<String, String>> set = map.entrySet();
		for (Entry<String, String> e : set) {
			params.addQueryStringParameter(e.getKey(), e.getValue());
		}

		handler = SingleInstanceUtils.getHttpUtilsInstance().send(
				HttpRequest.HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException error, String msg) {
						listener.onFailed(error, msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> result) {
						listener.onSuccessed(result.result);

					}

				});
	}

	public HttpHandler<String> getHttpHandler() {
		return handler;
	}

}
